<?php

namespace app\controller;


use app\BaseController;
use app\model\UserModel;
use think\App;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\exception\ValidateException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;

class User extends BaseController
{
    protected $userModel;

    public function __construct(App $app)
    {
        $this -> userModel = new UserModel();
        parent ::__construct($app);
    }

    /**
     * 注册用户
     * @return \think\response\Json
     */
    public function createUser()
    {
        if (!Request ::isPost()) {
            return $this -> errorPut('请求方式不对');
        }

        $getData = Request::only(['username', 'captcha','password'], 'post');
         try {
             $this -> validate($getData,'\app\validate\User.create');
         } catch (ValidateException $e) {
             // 验证失败 输出错误信息
             return  $this->errorPut($e->getMessage());
         }

        // 检测输入的验证码是否正确，$value为用户输入的验证码字符串
        if (!captcha_check($getData['captcha'])) {
            // 验证失败
            return $this -> errorPut('验证码输入有误');
        }
        unset($getData['captcha']);

        $getData['password'] = md5(md5($getData['password']));

        try {
            if ($this -> userModel -> where('username', '=', $getData['username']) -> find()) {
                return $this -> errorPut('用户名重复');
            }
        } catch (DataNotFoundException $e) {
            return $this -> errorPut($e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this -> errorPut($e->getMessage());
        } catch (DbException $e) {
            return $this -> errorPut($e->getMessage());
        }
        return $this -> outTrue($this -> userModel -> getCreate($getData));
    }

    /**
     * 修改用户
     * @return \think\response\Json
     */
    public function updateUser()
    {
        if (Request ::isPost()) {
            //修改用户信息
            $getData = Request ::only(['username','user_id', 'password'], 'post');

            try {
                $this -> validate($getData,'\app\validate\User.edit');
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return $this->errorPut($e->getMessage());
            }

            $getData['password'] = md5(md5($getData['password']));
            //判断用户唯一性
            try {
                $trWhere = [
                    ['user_id', '<>', $getData['user_id']],
                    ['username', '=', $getData['username']]
                ];
                if ($this->userModel->where($trWhere)->find()) {
                    return $this -> errorPut('用户名重复');
                }
            } catch (DataNotFoundException $e) {
                return $this -> errorPut($e->getMessage());
            } catch (ModelNotFoundException $e) {
                return $this -> errorPut($e->getMessage());
            } catch (DbException $e) {
                return $this -> errorPut($e->getMessage());
            }
            //修改操作
            $this->userModel->getUpdate($getData);
        } elseif (Request ::isGet()) {
            //获取用户信息
            $getData = Request ::only(['user_id'], 'get');

            try {
                $this -> validate($getData,'\app\validate\User.read');
            } catch (ValidateException $e) {

                // 验证失败 输出错误信息
                return $this->errorPut($e->getMessage());
            }
            $user = $this -> userModel -> getOne($getData['user_id']);
            return $this -> outTrue($user);
        } else {
            return $this -> errorPut('请求方式不对');
        }

    }


}
